package com.smartstate.dataManagement.timedtask;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.smartstate.dataManagement.constant.MeterEnum;
import com.smartstate.dataManagement.entity.*;
import com.smartstate.dataManagement.mapper.EquipmentMapper;
import com.smartstate.dataManagement.mapper.PowerEquipmentEntityDao;
import com.smartstate.dataManagement.mapper.PowerMeterSummaryMapper;
import com.smartstate.dataManagement.mapper.RateCalculationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

@Service
@Slf4j
public class AsyncSummaryTaskService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private PowerEquipmentEntityDao powerEquipmentEntityDao;

    @Autowired
    private PowerMeterSummaryMapper powerMeterSummaryMapper;

    @Autowired
    private RateCalculationMapper rateCalculationMapper;


    public boolean summaryTask30Minute(DateTime startDate, DateTime endDate) {
        String startDateStr = startDate.toString();
        String dayStr = startDate.toDateStr();
        String monthStr = DateUtil.format(startDate, "yyyy-MM");
        DateTime endOfWeek = DateUtil.endOfWeek(startDate);
        String weekStr = endOfWeek.toDateStr();
        String year = startDate.year() + "";
        String endDateStr = endDate.toString();
        //实体电表汇总
//        获取要查询电表的所有设备id集合
        List<Map<String, Object>> deviceIdList = equipmentMapper.queryDeviceId("电表");
        deviceIdList.forEach((deviceMap) -> {
//            判断查询的表是否存在
            String tableIsEmpty = powerEquipmentEntityDao.queryTableIsEmpty(String.valueOf(deviceMap.get("id")));
            if (tableIsEmpty != null){
                HalfHourPowerMeterSummaryEntity halfHourPowerMeterSummaryEntity = new HalfHourPowerMeterSummaryEntity();
                halfHourPowerMeterSummaryEntity.setId(UUID.randomUUID().toString());
                halfHourPowerMeterSummaryEntity.setDayStr(dayStr);
                halfHourPowerMeterSummaryEntity.setMonthStr(monthStr);
                halfHourPowerMeterSummaryEntity.setWeekStr(weekStr);
                halfHourPowerMeterSummaryEntity.setYearStr(year);
                halfHourPowerMeterSummaryEntity.setCreateTime(endDateStr);
                halfHourPowerMeterSummaryEntity.setMeterId(String.valueOf(deviceMap.get("deviceId")));
                halfHourPowerMeterSummaryEntity.setStartTime(startDateStr);
                halfHourPowerMeterSummaryEntity.setEndTime(endDateStr);
                halfHourPowerMeterSummaryEntity.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                halfHourPowerMeterSummaryEntity.setDetActiveEnergy(0);
                halfHourPowerMeterSummaryEntity.setDetApparentEnergy(0);
                halfHourPowerMeterSummaryEntity.setDetReactiveEnergy(0);
//            通过遍历电量参数的枚举值，获取各参数班小时的差值总和
                for (String attributeTD : MeterEnum.METER) {
//                判断TD中对应的表是否存在
                    String attributeValueStr = powerEquipmentEntityDao.querySum(startDateStr, endDateStr, String.valueOf(deviceMap.get("id")), attributeTD);
                    attributeValueStr = attributeValueStr == null ? "0" : attributeValueStr;
                    String attributeMysql = transfrom(attributeTD.toString());
                    invokeSet(halfHourPowerMeterSummaryEntity, attributeMysql, Float.parseFloat(attributeValueStr));
                }
                halfHourPowerMeterSummaryEntity.setObjId(String.valueOf(deviceMap.get("id")));
//            通过费率计算价格
                RateCalculation rateCalculation = rateCalculationMapper.queryRate(startDateStr, "电");
//                判断是否当前时间获取到费率信息
                if(rateCalculation != null){
                    halfHourPowerMeterSummaryEntity.setRateType(rateCalculation.getMedium());
//            获取时间判断当前统计时间是否在起止时间之内
                    List<Map> lists = JSON.parseArray(rateCalculation.getDetailed(), Map.class);
                    String[] dateStartAndEnd = null;
                    List<Map> rateTimeList = null;
                    for (Map map : lists) {
                        if(map.containsKey("timer")){
                            String[] times = map.get("timer").toString().split(",");
//                    判断是否包含当前时间
                            if(dayStr.compareTo(times[0]) >= 0 && times[1].compareTo(dayStr) >= 0){
                                dateStartAndEnd = map.get("timer").toString().split(",");
                                if(map.containsKey("rateTime")){
                                    rateTimeList =(List<Map>) map.get("rateTime");
                                }
                            }
                        }
                    }
                    LocalDate startDateMysql = LocalDate.parse(dateStartAndEnd[0], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate endDateMysql = LocalDate.parse(dateStartAndEnd[1], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate dayStartDate = LocalDate.parse(dayStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    long betweenStart = ChronoUnit.DAYS.between(startDateMysql, dayStartDate);
                    long betweenEnd = ChronoUnit.DAYS.between(dayStartDate, endDateMysql);
                    if (betweenStart >= 0 && betweenEnd >= 0){
//                获取当前时间在当天的费率价格
                        AtomicBoolean keepGoing = new AtomicBoolean(true);
                        rateTimeList.forEach((item) -> {
                            if (keepGoing.get()) {
                                String[] dayStartAndEndTime = item.get("rateTimer").toString().split(",");
                                DateTime dayStartTime = DateUtil.parse(dayStartAndEndTime[0], "HH:mm");
                                DateTime dayEndTime = DateUtil.parse(dayStartAndEndTime[1], "HH:mm");
                                String currentTimeStr = DateUtil.format(startDate, "HH:mm");
                                DateTime currentTime = DateUtil.parse(currentTimeStr, "HH:mm");
                                long betweenDayStart = ChronoUnit.SECONDS.between(dayStartTime.toInstant(), currentTime.toInstant());
                                long betweenDayEnd = ChronoUnit.SECONDS.between(currentTime.toInstant(), dayEndTime.toInstant());
                                if (betweenDayEnd >= 0 && betweenDayStart >= 0) {
                                    float price = Float.valueOf(item.get("Price").toString());
                                    halfHourPowerMeterSummaryEntity.setPrice(price);
                                    float totalPrice = price * halfHourPowerMeterSummaryEntity.getDetActiveEnergy();
                                    halfHourPowerMeterSummaryEntity.setTotalPrice(totalPrice);
                                    switch(String.valueOf(item.get("item"))){
                                        case "平值":
                                            halfHourPowerMeterSummaryEntity.setFlatValue(totalPrice);
                                            halfHourPowerMeterSummaryEntity.setValleyValue(0);
                                            halfHourPowerMeterSummaryEntity.setPeekValue(0);
                                            break;
                                        case "峰值":
                                            halfHourPowerMeterSummaryEntity.setFlatValue(0);
                                            halfHourPowerMeterSummaryEntity.setValleyValue(0);
                                            halfHourPowerMeterSummaryEntity.setPeekValue(totalPrice);
                                            break;
                                        default:
                                            halfHourPowerMeterSummaryEntity.setFlatValue(0);
                                            halfHourPowerMeterSummaryEntity.setValleyValue(totalPrice);
                                            halfHourPowerMeterSummaryEntity.setPeekValue(0);
                                    }
                                    keepGoing.set(false);
                                }
                            }
                        });
                    }
//            将得到的设备电费半小时统计量保存到数据库中
                    powerMeterSummaryMapper.saveMeter(halfHourPowerMeterSummaryEntity);
                }
            }
        });


//        实体水表查询
        List<Map<String, Object>> waterDeviceIdList = equipmentMapper.queryDeviceId("水表");
        waterDeviceIdList.forEach((deviceMap) -> {
//            判断查询的表是否存在
            String tableIsEmpty = powerEquipmentEntityDao.queryTableIsEmpty(String.valueOf(deviceMap.get("id")));
            if (tableIsEmpty != null){
                HalfHourFlowMeterSummaryEntity halfHourFlowMeterSummaryEntity = new HalfHourFlowMeterSummaryEntity();
                halfHourFlowMeterSummaryEntity.setDeviceId(String.valueOf(deviceMap.get("deviceId")));
                halfHourFlowMeterSummaryEntity.setDayStr(dayStr);
                halfHourFlowMeterSummaryEntity.setCreateTime(endDateStr);
                halfHourFlowMeterSummaryEntity.setMonthStr(monthStr);
                halfHourFlowMeterSummaryEntity.setWeekStr(weekStr);
                halfHourFlowMeterSummaryEntity.setYearStr(year);
                halfHourFlowMeterSummaryEntity.setId(UUID.randomUUID().toString());
                halfHourFlowMeterSummaryEntity.setStartTime(startDateStr);
                halfHourFlowMeterSummaryEntity.setEndTime(endDateStr);
                halfHourFlowMeterSummaryEntity.setDetTotal(0);
////            通过遍历电量参数的枚举值，获取各参数班小时的差值总和
                for (String attributeTD : MeterEnum.WATER) {
                    String attributeValueStr = powerEquipmentEntityDao.querySum(startDateStr, endDateStr, String.valueOf(deviceMap.get("id")), attributeTD);
                    String attributeMysql = transfrom(attributeTD.toString());
                    attributeValueStr = attributeValueStr == null ? "0" : attributeValueStr;
                    invokeSet(halfHourFlowMeterSummaryEntity, attributeMysql, Float.parseFloat(attributeValueStr));
                }
                halfHourFlowMeterSummaryEntity.setObjId(String.valueOf(deviceMap.get("id")));
//            通过费率计算价格
                RateCalculation rateCalculation = rateCalculationMapper.queryRate(startDateStr, "水");
                if(rateCalculation != null){
                    halfHourFlowMeterSummaryEntity.setRateType(rateCalculation.getMedium());
//            获取时间判断当前统计时间是否在起止时间之内
                    List<Map> lists = JSON.parseArray(rateCalculation.getDetailed(), Map.class);
                    String[] dateStartAndEnd = null;
                    List<Map> rateTimeList = null;
                    for (Map map : lists) {
                        if(map.containsKey("timer")){
                            String[] times = map.get("timer").toString().split(",");
//                    判断是否包含当前时间
                            if(dayStr.compareTo(times[0]) >= 0 && times[1].compareTo(dayStr) >= 0){
                                dateStartAndEnd = map.get("timer").toString().split(",");
                                if(map.containsKey("rateTime")){
                                    rateTimeList =(List<Map>) map.get("rateTime");
                                }
                            }
                        }
                    }
                    LocalDate startDateMysql = LocalDate.parse(dateStartAndEnd[0], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate endDateMysql = LocalDate.parse(dateStartAndEnd[1], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate dayStartDate = LocalDate.parse(dayStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    long betweenStart = ChronoUnit.DAYS.between(startDateMysql, dayStartDate);
                    long betweenEnd = ChronoUnit.DAYS.between(dayStartDate, endDateMysql);
////            获取时间判断当前统计时间是否在起止时间之内
                    if (betweenStart >= 0 && betweenEnd >= 0){
//                获取当前时间在当天的费率价格
                        AtomicBoolean keepGoing = new AtomicBoolean(true);
                        rateTimeList.forEach((item) -> {
                            if (keepGoing.get()) {
                                String[] dayStartAndEndTime = item.get("rateTimer").toString().split(",");
                                DateTime dayStartTime = DateUtil.parse(dayStartAndEndTime[0], "HH:mm");
                                DateTime dayEndTime = DateUtil.parse(dayStartAndEndTime[1], "HH:mm");
                                String currentTimeStr = DateUtil.format(startDate, "HH:mm");
                                DateTime currentTime = DateUtil.parse(currentTimeStr, "HH:mm");
                                long betweenDayStart = ChronoUnit.SECONDS.between(dayStartTime.toInstant(), currentTime.toInstant());
                                long betweenDayEnd = ChronoUnit.SECONDS.between(currentTime.toInstant(), dayEndTime.toInstant());
                                if (betweenDayEnd >= 0 && betweenDayStart >= 0) {
                                    float price = Float.valueOf(item.get("Price").toString());
                                    halfHourFlowMeterSummaryEntity.setPrice(price);
                                    float totalPrice = price * halfHourFlowMeterSummaryEntity.getDetTotal();
                                    halfHourFlowMeterSummaryEntity.setTotalPrice(totalPrice);
                                    switch(String.valueOf(item.get("item"))){
                                        case "平值":
                                            halfHourFlowMeterSummaryEntity.setFlatValue(totalPrice);
                                            halfHourFlowMeterSummaryEntity.setValleyValue(0);
                                            halfHourFlowMeterSummaryEntity.setPeekValue(0);
                                            break;
                                        case "峰值":
                                            halfHourFlowMeterSummaryEntity.setFlatValue(0);
                                            halfHourFlowMeterSummaryEntity.setValleyValue(0);
                                            halfHourFlowMeterSummaryEntity.setPeekValue(totalPrice);
                                            break;
                                        default:
                                            halfHourFlowMeterSummaryEntity.setFlatValue(0);
                                            halfHourFlowMeterSummaryEntity.setValleyValue(totalPrice);
                                            halfHourFlowMeterSummaryEntity.setPeekValue(0);
                                    }
                                    keepGoing.set(false);
                                }
                            }
                        });
                    }
////            将得到的设备水费半小时统计量保存到数据库中
                    powerMeterSummaryMapper.saveWater(halfHourFlowMeterSummaryEntity);
                }
            }
        });

//
//        实体天然气查询
        List<Map<String, Object>> naturalGasDeviceIdList = equipmentMapper.queryDeviceId("天然气表");
        naturalGasDeviceIdList.forEach((deviceMap) -> {
           //            判断查询的表是否存在
            String tableIsEmpty = powerEquipmentEntityDao.queryTableIsEmpty(String.valueOf(deviceMap.get("id")));
            if (tableIsEmpty != null){
                HalfHourNaturalGasSummaryEntity halfHourNaturalGasSummaryEntity = new HalfHourNaturalGasSummaryEntity();
                halfHourNaturalGasSummaryEntity.setDeviceId(String.valueOf(deviceMap.get("deviceId")));
                halfHourNaturalGasSummaryEntity.setDayStr(dayStr);
                halfHourNaturalGasSummaryEntity.setCreateTime(endDateStr);
                halfHourNaturalGasSummaryEntity.setMonthStr(monthStr);
                halfHourNaturalGasSummaryEntity.setWeekStr(weekStr);
                halfHourNaturalGasSummaryEntity.setYearStr(year);
                halfHourNaturalGasSummaryEntity.setId(UUID.randomUUID().toString());
                halfHourNaturalGasSummaryEntity.setStartTime(startDateStr);
                halfHourNaturalGasSummaryEntity.setEndTime(endDateStr);
                halfHourNaturalGasSummaryEntity.setTotalPrice(0);
////            通过遍历电量参数的枚举值，获取各参数班小时的差值总和
                for (String attributeTD : MeterEnum.NATURALGAS) {
                    String attributeValueStr = powerEquipmentEntityDao.querySum(startDateStr, endDateStr, String.valueOf(deviceMap.get("id")), attributeTD);
                    String attributeMysql = transfrom(attributeTD.toString());
                    attributeValueStr = attributeValueStr == null ? "0" : attributeValueStr;
                    invokeSet(halfHourNaturalGasSummaryEntity, attributeMysql, Float.parseFloat(attributeValueStr));
                }
                halfHourNaturalGasSummaryEntity.setObjId(String.valueOf(deviceMap.get("id")));
//            通过费率计算价格
                RateCalculation rateCalculation = rateCalculationMapper.queryRate(startDateStr, "天然气");
//                判断是否当前时间获取到费率信息
                if(rateCalculation != null){
                    halfHourNaturalGasSummaryEntity.setRateType(rateCalculation.getMedium());
//            获取时间判断当前统计时间是否在起止时间之内
                    List<Map> lists = JSON.parseArray(rateCalculation.getDetailed(), Map.class);
                    String[] dateStartAndEnd = null;
                    List<Map> rateTimeList = null;
                    for (Map map : lists) {
                        if(map.containsKey("timer")){
                            String[] times = map.get("timer").toString().split(",");
//                    判断是否包含当前时间
                            if(dayStr.compareTo(times[0]) >= 0 && times[1].compareTo(dayStr) >= 0){
                                dateStartAndEnd = map.get("timer").toString().split(",");
                                if(map.containsKey("rateTime")){
                                    rateTimeList =(List<Map>) map.get("rateTime");
                                }
                            }
                        }
                    }
                    LocalDate startDateMysql = LocalDate.parse(dateStartAndEnd[0], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate endDateMysql = LocalDate.parse(dateStartAndEnd[1], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate dayStartDate = LocalDate.parse(dayStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    long betweenStart = ChronoUnit.DAYS.between(startDateMysql, dayStartDate);
                    long betweenEnd = ChronoUnit.DAYS.between(dayStartDate, endDateMysql);
////            获取时间判断当前统计时间是否在起止时间之内
                    if (betweenStart >= 0 && betweenEnd >= 0){
//                获取当前时间在当天的费率价格
                        AtomicBoolean keepGoing = new AtomicBoolean(true);
                        rateTimeList.forEach((item) -> {
                            if (keepGoing.get()) {
                                String[] dayStartAndEndTime = item.get("rateTimer").toString().split(",");
                                DateTime dayStartTime = DateUtil.parse(dayStartAndEndTime[0], "HH:mm");
                                DateTime dayEndTime = DateUtil.parse(dayStartAndEndTime[1], "HH:mm");
                                String currentTimeStr = DateUtil.format(startDate, "HH:mm");
                                DateTime currentTime = DateUtil.parse(currentTimeStr, "HH:mm");
                                long betweenDayStart = ChronoUnit.SECONDS.between(dayStartTime.toInstant(), currentTime.toInstant());
                                long betweenDayEnd = ChronoUnit.SECONDS.between(currentTime.toInstant(), dayEndTime.toInstant());
                                if (betweenDayEnd >= 0 && betweenDayStart >= 0) {
                                    float price = Float.valueOf(item.get("Price").toString());
                                    halfHourNaturalGasSummaryEntity.setPrice(price);
                                    float totalPrice = price * halfHourNaturalGasSummaryEntity.getDetTotal();
                                    halfHourNaturalGasSummaryEntity.setTotalPrice(totalPrice);
                                    switch(String.valueOf(item.get("item"))){
                                        case "平值":
                                            halfHourNaturalGasSummaryEntity.setFlatValue(totalPrice);
                                            halfHourNaturalGasSummaryEntity.setValleyValue(0);
                                            halfHourNaturalGasSummaryEntity.setPeekValue(0);
                                            break;
                                        case "峰值":
                                            halfHourNaturalGasSummaryEntity.setFlatValue(0);
                                            halfHourNaturalGasSummaryEntity.setValleyValue(0);
                                            halfHourNaturalGasSummaryEntity.setPeekValue(totalPrice);
                                            break;
                                        default:
                                            halfHourNaturalGasSummaryEntity.setFlatValue(0);
                                            halfHourNaturalGasSummaryEntity.setValleyValue(totalPrice);
                                            halfHourNaturalGasSummaryEntity.setPeekValue(0);
                                    }
                                    keepGoing.set(false);
                                }
                            }
                        });
                    }
////            将得到的设备天然气半小时统计量保存到数据库中
                    powerMeterSummaryMapper.saveNaturalGas(halfHourNaturalGasSummaryEntity);
                }
            }
        });

        //        实体水蒸汽气查询
        List<Map<String, Object>> vapourDeviceIdList = equipmentMapper.queryDeviceId("水蒸气表");
        vapourDeviceIdList.forEach((deviceMap) -> {
//            判断查询的表是否存在
            String tableIsEmpty = powerEquipmentEntityDao.queryTableIsEmpty(String.valueOf(deviceMap.get("id")));
            if (tableIsEmpty != null){
                HalfHourVapourSummaryEntity halfHourVapourSummaryEntity = new HalfHourVapourSummaryEntity();
                halfHourVapourSummaryEntity.setDeviceId(String.valueOf(deviceMap.get("deviceId")));
                halfHourVapourSummaryEntity.setDayStr(dayStr);
                halfHourVapourSummaryEntity.setCreateTime(endDateStr);
                halfHourVapourSummaryEntity.setMonthStr(monthStr);
                halfHourVapourSummaryEntity.setWeekStr(weekStr);
                halfHourVapourSummaryEntity.setYearStr(year);
                halfHourVapourSummaryEntity.setId(UUID.randomUUID().toString());
                halfHourVapourSummaryEntity.setStartTime(startDateStr);
                halfHourVapourSummaryEntity.setEndTime(endDateStr);
                halfHourVapourSummaryEntity.setTotalPrice(0);
////            通过遍历电量参数的枚举值，获取各参数班小时的差值总和
                for (String attributeTD : MeterEnum.WATER) {
                    String attributeValueStr = powerEquipmentEntityDao.querySum(startDateStr, endDateStr, String.valueOf(deviceMap.get("id")), attributeTD);
                    attributeValueStr = attributeValueStr == null ? "0" : attributeValueStr;
                    String attributeMysql = transfrom(attributeTD.toString());
                    invokeSet(halfHourVapourSummaryEntity, attributeMysql, Float.parseFloat(attributeValueStr));
                }
                halfHourVapourSummaryEntity.setObjId(String.valueOf(deviceMap.get("id")));
//            通过费率计算价格
                RateCalculation rateCalculation = rateCalculationMapper.queryRate(startDateStr, "水蒸气");
//                判断是否当前时间获取到费率信息
                if(rateCalculation != null){

                    halfHourVapourSummaryEntity.setRateType(rateCalculation.getMedium());
//            获取时间判断当前统计时间是否在起止时间之内
                    List<Map> lists = JSON.parseArray(rateCalculation.getDetailed(), Map.class);
                    String[] dateStartAndEnd = null;
                    List<Map> rateTimeList = null;
                    for (Map map : lists) {
                        if(map.containsKey("timer")){
                            String[] times = map.get("timer").toString().split(",");
//                    判断是否包含当前时间
                            if(dayStr.compareTo(times[0]) >= 0 && times[1].compareTo(dayStr) >= 0){
                                dateStartAndEnd = map.get("timer").toString().split(",");
                                if(map.containsKey("rateTime")){
                                    rateTimeList =(List<Map>) map.get("rateTime");
                                }
                            }
                        }
                    }
                    LocalDate startDateMysql = LocalDate.parse(dateStartAndEnd[0], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate endDateMysql = LocalDate.parse(dateStartAndEnd[1], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    LocalDate dayStartDate = LocalDate.parse(dayStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    long betweenStart = ChronoUnit.DAYS.between(startDateMysql, dayStartDate);
                    long betweenEnd = ChronoUnit.DAYS.between(dayStartDate, endDateMysql);
////            获取时间判断当前统计时间是否在起止时间之内
                    if (betweenStart >= 0 && betweenEnd >= 0){
//                获取当前时间在当天的费率价格
                        AtomicBoolean keepGoing = new AtomicBoolean(true);
                        rateTimeList.forEach((item) -> {
                            if (keepGoing.get()) {
                                String[] dayStartAndEndTime = item.get("rateTimer").toString().split(",");
                                DateTime dayStartTime = DateUtil.parse(dayStartAndEndTime[0], "HH:mm");
                                DateTime dayEndTime = DateUtil.parse(dayStartAndEndTime[1], "HH:mm");
                                String currentTimeStr = DateUtil.format(startDate, "HH:mm");
                                DateTime currentTime = DateUtil.parse(currentTimeStr, "HH:mm");
                                long betweenDayStart = ChronoUnit.SECONDS.between(dayStartTime.toInstant(), currentTime.toInstant());
                                long betweenDayEnd = ChronoUnit.SECONDS.between(currentTime.toInstant(), dayEndTime.toInstant());
                                if (betweenDayEnd >= 0 && betweenDayStart >= 0) {
                                    float price = Float.valueOf(item.get("Price").toString());
                                    halfHourVapourSummaryEntity.setPrice(price);
                                    float totalPrice = price * halfHourVapourSummaryEntity.getDetTotal();
                                    halfHourVapourSummaryEntity.setTotalPrice(totalPrice);
                                    switch(String.valueOf(item.get("item"))){
                                        case "平值":
                                            halfHourVapourSummaryEntity.setFlatValue(totalPrice);
                                            halfHourVapourSummaryEntity.setValleyValue(0);
                                            halfHourVapourSummaryEntity.setPeekValue(0);
                                            break;
                                        case "峰值":
                                            halfHourVapourSummaryEntity.setFlatValue(0);
                                            halfHourVapourSummaryEntity.setValleyValue(0);
                                            halfHourVapourSummaryEntity.setPeekValue(totalPrice);
                                            break;
                                        default:
                                            halfHourVapourSummaryEntity.setFlatValue(0);
                                            halfHourVapourSummaryEntity.setValleyValue(totalPrice);
                                            halfHourVapourSummaryEntity.setPeekValue(0);
                                    }
                                    keepGoing.set(false);
                                }
                            }
                        });
                    }
////            将得到的设备水蒸气半小时统计量保存到数据库中
                    powerMeterSummaryMapper.saveVapour(halfHourVapourSummaryEntity);
                }
            }
        });
        return true;
    }

    /**
     * java反射bean的set方法
     *
     * @param objectClass
     * @param fieldName
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Method getSetMethod(Class objectClass, String fieldName) {
        try {
            Class[] parameterTypes = new Class[1];
            Field field = objectClass.getDeclaredField(fieldName);
            parameterTypes[0] = field.getType();
            StringBuffer sb = new StringBuffer();
            sb.append("set");
            sb.append(fieldName.substring(0, 1).toUpperCase());
            sb.append(fieldName.substring(1));
            Method method = objectClass.getMethod(sb.toString(), parameterTypes);
            return method;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 执行set方法
     *
     * @param o 执行对象
     * @param fieldName 属性
     * @param value 值
     */
    public static void invokeSet(Object o, String fieldName, Object value) {
        Method method = getSetMethod(o.getClass(), fieldName);
        try {
            method.invoke(o, new Object[] { value });
        } catch (Exception e) {
            log.info("参数输入有误，无法读取数据");
            e.printStackTrace();
        }
    }

    /**
     * 去掉下划线，并把首字母转大写
     * @param attribute
     * @return
     */
    public String transfrom(String attribute){
        StringBuilder sb = new StringBuilder();
        String[] strings = attribute.split("_");
        sb.append(strings[0]);
        for (int i = 1; i < strings.length; i++){
            sb.append(strings[i].substring(0, 1).toUpperCase());
            sb.append(strings[i].substring(1));
        }
        return sb.toString();
    }
}
